package com.cz.mes.view;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UpdateColumnComment {
    public static void main(String[] args) {
        String dbGroup = "uat3";
        try {
            int tabled_id = 15;
            String sql = "select * from mes_vw_column where table_id =:tabled_id and status =1";
            Map<String, Object> selectParams = new HashMap<>();
            selectParams.put("tabled_id", tabled_id);
            List<Entity> entityList = Db.use(dbGroup).query(sql,selectParams);

            for (Entity entity : entityList) {
                String columnId = entity.getStr("id");

                String dbColumnName = entity.getStr("db_column_name");
                if (StrUtil.indexOfIgnoreCase(dbColumnName, ".") != -1) {
                    dbColumnName = StrUtil.subAfter(dbColumnName, ".", true);
                }
                String sql1 = "SELECT COLUMN_NAME, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'mesdb' AND COLUMN_NAME = :columnName";
                Map<String,Object> params = new HashMap<>();
                params.put("columnName",dbColumnName);
                Entity columnEntity = Db.use(dbGroup).queryOne(sql1,params);
                String columnName =columnEntity.getStr("COLUMN_NAME");
                String columnComment = columnEntity.getStr("COLUMN_COMMENT");
                columnComment = StrUtil.subBefore(columnComment, "：", false);
                System.out.println(columnId+" "+columnComment);

                String sql2  = "UPDATE mes_vw_column SET column_title = :columnComment WHERE id = :columnId";
                Map<String,Object> updateParams = new HashMap<>();
                updateParams.put("columnId",columnId);
                updateParams.put("columnComment",columnComment);
                Db.use(dbGroup).execute(sql2,updateParams);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
